<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../js/layui/css/layui.css" media="all">
    <style type="text/css">
        .layui-table-cell{height:40px;line-height:40px;}
    </style>
</head>
<body>

<blockquote class="layui-elem-quote quoteBox mod_default_box">
    <form class="layui-form">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" id="stu_name" name="stu_name" placeholder="请输入关键字" autocomplete="off" class="layui-input" />
            </div>
            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search-stu-submit">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
        </div>
    </form>
</blockquote>
<div style="display: none" id="batchAdd"></div>
<!--数据列表-->
<table class="layui-hide" id="test" lay-filter="stu-table"></table>
<script type="text/html" id="tb">
    <div class="layui-container" style="margin-left:0px;">
        <a class="layui-btn layui-btn-xs" lay-event="batchAdd"><i class="layui-icon layui-icon-addition"></i>批量导入商户</a>
        <a class="layui-btn layui-btn-xs" id="exportExcel">导出Excel</a>
    </div>
</script>
<script type="text/html" id="stu-toolbar">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="resetPwd">重置密码</a>
    <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>-->
</script>
<script type="text/html" id="checkboxTpl">
    <input type="checkbox" name="status" value="{{d.id}}" title="启用" lay-filter="lockDemo" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script src="../../js/layui/layui.js"></script>
<script src="../../js/layui/layui.all.js"></script>
<script src="../../js/token.js"></script>
<script src="../../js/config.js"></script>

<script>
    layui.use(['table','upload'], function(){
        var $ = layui.$;
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var upload = layui.upload;
        table.render({
            elem: '#test'
            ,id:'stu-table-id'//重载需要
            ,url:HOST+'business/getBusinessList'
            ,title:'商户表'
            ,toolbar: '#tb'
            ,cols: [[
                {type:'numbers', title: '序号'}
                ,{field:'name',align : 'center',width : 180, title: '商户名称'}
                ,{field:'bh',align : 'center',width : 125,title: '商户编号'}
                ,{field:'fzr',width : 110,align : 'center', title: '负责人'}
                ,{field:'address',align : 'center', width : 125,title: '商户地址'}
                ,{field:'wyName',width : 200,align : 'center', title: '所属物业', templet:function(res){
                        if(res.wyName != undefined) {
                            return res.wyName
                        }else{
                            return ""
                        }
                    }}
                ,{field:'message',align : 'center', width : 125,title: '备注', templet: function (res) {
                    if (res.message != null && res.message != "" && res.message != undefined && res.message != "null") {
                        if (res.message.indexOf("<") != -1) {
                            res.message = res.message.replace("<", "[");
                        }
                        if (res.message.indexOf(">") != -1) {
                            res.message = res.message.replace(">", "]");
                        }
                        return res.message
                    }else {
                        return "";
                    }
                }}
                ,{field:'status',align : 'center', title:'是否启用',width:110, templet: '#checkboxTpl', sort: true, fixed: 'right'}
                ,{toolbar:"#stu-toolbar",align : 'center',width:160,title:"操作",fixed: 'right'}
            ]]
            ,page: true
            ,page: {
                curr: layui.data("business_page").index
            },
            done: (res, curr, count) => {
                layui.data("business_page", {
                    key: 'index',
                    value: curr
                });
                bindClick();
            }
        });

        //搜索操作
        form.on('submit(search-stu-submit)',function (data) {
            table.reload('stu-table-id',{
                where:{"name":data.field.stu_name}
                ,page:{
                    curr:1
                }
            });
            return false;
        });

        //表操作  监听操作
        table.on('tool(stu-table)',function (obj) {
            var data=obj.data;
            var event=obj.event;
            if(event==='edit'){
                layer.open({
                    type:2
                    ,title:"编辑商户信息"
                    ,skin:'layui-layer-molv'
                    ,area: ['95%', '95%'],
                    content:("edit_business.html?id="+data.id+"&name="+encodeURIComponent(data.name)+"&fzr="+data.fzr+"&bh="+data.bh+"&status="+data.status
                        +"&address="+encodeURIComponent(data.address)+"&wyid="+data.wyid+"&wyName="+encodeURIComponent(data.wyName)+"&message="+encodeURIComponent(data.message))
                    //content:$('#update-stu-layer')
                });
            }else if(obj.event == 'resetPwd'){
                layer.confirm("确定重置密码？",function (index) {
                    $.ajax({
                        url: HOST+"business/resetPwd?id=" + data.id
                        , type: "GET"
                        , success: function (data) {
                            if (data.code == 200) {
                                layer.msg("密码重置成功");
                                layer.close(index);
                                table.reload("stu-table-id");
                            } else if (data.code == 500) {
                                layer.msg("密码重置失败");
                            }else {
                                layer.msg("拒绝访问");
                            }
                        }
                        , error: function () {
                            console.log("ajax error");
                        }
                    });
                })
            }
        });

        //监听启用操作
        form.on('checkbox(lockDemo)', function(obj){
            var id = this.value;
            var status = obj.elem.checked;
            var zhuangtai;
            if(status){
                zhuangtai=1;
            }else{
                zhuangtai=0;
            }
            $.ajax({
                url:HOST+"business/upStatus"
                ,type:"get"
                ,data:{id:id,status:zhuangtai}
                ,contentType: 'application/json'
                ,success:function (data) {
                    if(data.code==0){
                        layer.close(layer.index);
                        layer.msg('操作成功');
                    }else if(data.code==1){
                        layer.msg('操作失败');
                    }else{
                        layer.msg('没有权限');
                    }
                }
                ,error:function () {
                    console.log("ajax error");
                }
            });
        });

        var uploadInst = upload.render({
            elem: '#batchAdd'
            , url: HOST+"business/batchAddBusiness"
            , accept: "file",
            shade: [0.5,'#000'],
            exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
            , before: function (obj) {
                layui.layer.load();
            }
            , done: function (res) {
                if (res.code == 200) {
                    layer.msg("导入成功！");
                    table.reload("stu-table-id");
                    var  index = layui.layer.load();
                    layer.close(index);
                }
                else {
                    layer.alert(res.data, {
                        icon: 2,
                        skin: 'layer-ext-demo'
                    })
                    var  index = layui.layer.load();
                    layer.close(index);
                }

            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        function bindClick() {
            $('#exportExcel').on('click',function () {
                var stu_name = $("#stu_name").val();
                $("#exportExcel").attr('href',HOST+"business/exportExcelByBusiness?name=" + stu_name);
            });
        };

        table.on('toolbar(stu-table)', function (obj) {
            var data=obj.data;
            var event = obj.event;
            if (event === 'batchAdd') {
                $("#batchAdd").click();
            }
        })
    });
</script>

</body>
</html>